package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.constant.CacheConstant;
import cn.com.dcsgo.dto.NoticeAddDTO;
import cn.com.dcsgo.dto.NoticeQueryDTO;
import cn.com.dcsgo.dto.NoticeUpdateDTO;
import cn.com.dcsgo.dto.PageDTO;
import cn.com.dcsgo.exception.BusinessException;
import cn.com.dcsgo.utils.AuthUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.Notice;
import cn.com.dcsgo.service.NoticeService;
import cn.com.dcsgo.mapper.NoticeMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 针对表【notice】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 2025-10-26 13:09:56
 */
@Service
@CacheConfig(cacheNames = "cn.com.dcsgo.service.impl.NoticeServiceImpl")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {

    @Override
    public Page<Notice> getPage(PageDTO pageDTO, NoticeQueryDTO noticeQueryDTO) {
        Long shopId = AuthUtil.getShopId();
        Boolean isTop = noticeQueryDTO.getIsTop();
        Integer status = noticeQueryDTO.getStatus();
        String title = noticeQueryDTO.getTitle();
        Page<Notice> page = new Page<>(pageDTO.getCurrent(), pageDTO.getSize());
        return baseMapper.selectPage(page,
                new LambdaQueryWrapper<Notice>()
                        .eq(shopId == null || !shopId.equals(1L), Notice::getShopId, shopId)
                        .eq(isTop != null, Notice::getIsTop, isTop)
                        .eq(status != null, Notice::getStatus, status)
                        .like(StringUtils.hasText(title), Notice::getTitle, title)
                        .orderByDesc(Notice::getCreateTime)
        );
    }

    @Override
    @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_NOTICE)
    public boolean save(NoticeAddDTO noticeAddDTO) {
        Long shopId = AuthUtil.getShopId();
        if (shopId == null) {
            throw new BusinessException("请先关联店铺后再操作");
        }
        if (noticeAddDTO.getStatus() == null) {
            noticeAddDTO.setStatus(1);
        }
        if (noticeAddDTO.getIsTop() == null) {
            noticeAddDTO.setIsTop(0);
        }
        Notice saveData = new Notice();
        BeanUtil.copyProperties(noticeAddDTO, saveData);
        saveData.setShopId(shopId);
        int row = baseMapper.insert(saveData);
        return row > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_NOTICE),
            @CacheEvict(key = "#noticeUpdateDTO.id")
    })
    public boolean update(NoticeUpdateDTO noticeUpdateDTO) {
        Notice updateData = new Notice();
        BeanUtil.copyProperties(noticeUpdateDTO, updateData);
        int row = baseMapper.updateById(updateData);
        return row > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_NOTICE),
            @CacheEvict(key = "#noticeId")
    })
    public boolean removeById(Long noticeId) {
        return super.removeById(noticeId);
    }

    @Override
    @Cacheable(key = CacheConstant.ALL_SUPPER_STORE_NORMAL_NOTICE)
    public List<Notice> getSupperNoticeList() {
        return list(
                new LambdaQueryWrapper<Notice>()
                        .eq(Notice::getStatus, 1)
                        .eq(Notice::getShopId, 1)
                        .orderByDesc(Notice::getIsTop, Notice::getUpdateTime)
        );
    }

    @Override
    @Cacheable(key = "#noticeId")
    public Notice getById(Long noticeId) {
        return super.getById(noticeId);
    }
}




